package ar.Location.Map;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import ar.Location.Api.GooglePlace.GooglePlace;
import ar.Location.Constant.Define;
import ar.Location.Helper.BitmapProvider;
import ar.Location.PlaceObject.Place;
import ar.Location.Route.Road;
import ar.Location.Route.RoutePoint;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class MapOverlay extends Overlay {
	static Road mRoad = null;
	EnhancedMapView roadMapView = null;
	Paint paintDraw = null;
	MyGeocoder myGeoCoder = null;
	public static ArrayList<GeoPoint> mPoints = new ArrayList<GeoPoint>();
	private Drawable markerIcon = null;

	public MapOverlay(EnhancedMapView mv, Context context) {

		roadMapView = mv;

		markerIcon = BitmapProvider.directionMarker;

		if (paintDraw == null) {
			paintDraw = new Paint();
			paintDraw.setAntiAlias(true);
			paintDraw.setColor(Color.BLUE);
			paintDraw.setStyle(Paint.Style.STROKE);
			paintDraw.setStrokeWidth(10);
			paintDraw.setAlpha(50);
		}

		if (myGeoCoder == null) {
			myGeoCoder = new MyGeocoder(context);
		}
	}

	@Override
	public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
		super.draw(canvas, mv, shadow);
		drawPath(mv, canvas);
		return true;
	}

	public static void setRoad(Road road) {
		if (mRoad != null) {
			mRoad.mPoints = null;
			mRoad.mRoute = null;
			mRoad = null;
			System.gc();
		}
		mRoad = road;
	}

	public void updateRoad() {

		if (mRoad != null) {

			mPoints.clear();

			if (mRoad.mRoute.length > 0) {
				roadMapView.clearMarkers();

				for (int i = 0; i < mRoad.mRoute.length; i++) {
					GeoPoint geoPoint = new GeoPoint(
							(int) (mRoad.mRoute[i][1] * 1E6),
							(int) (mRoad.mRoute[i][0] * 1E6));
					mPoints.add(geoPoint);
				}

				for (int j = 0; j < mRoad.mPoints.length; j++) {
					RoutePoint routePoint = mRoad.mPoints[j];
					if (routePoint != null) {
						if (routePoint.mLatitude != 0
								&& routePoint.mLongitude != 0) {
							GeoPoint geoPoint = new GeoPoint(
									((int) (routePoint.mLatitude * 1E6)),
									((int) (routePoint.mLongitude * 1E6)));
							MyOverlayItem myOverlayItem = new MyOverlayItem(
									geoPoint, routePoint.mName,
									routePoint.mDescription);
							Place place = new GooglePlace(
									Define.DS_GOOGLE_PLACE);
							place.name = routePoint.mName;
							place.address = routePoint.mDescription;
							place.latitude = String
									.valueOf(routePoint.mLatitude);
							place.longitude = String
									.valueOf(routePoint.mLongitude);
							place.imageUrl = routePoint.mIconUrl;
							myOverlayItem.updateInfoFrom(place);
							myOverlayItem.setMarker(markerIcon);
							roadMapView.addMarker(myOverlayItem);
						}
					}
				}

				setCenterRoad();
			}
		}
	}

	public void setCenterRoad() {
		int moveToLat = (mPoints.get(0).getLatitudeE6() + (mPoints.get(
				mPoints.size() - 1).getLatitudeE6() - mPoints.get(0)
				.getLatitudeE6()) / 2);
		int moveToLong = (mPoints.get(0).getLongitudeE6() + (mPoints.get(
				mPoints.size() - 1).getLongitudeE6() - mPoints.get(0)
				.getLongitudeE6()) / 2);

		GeoPoint moveTo = new GeoPoint(moveToLat, moveToLong);

		MapController mapController = roadMapView.getController();
		mapController.animateTo(moveTo);
		mapController.setZoom(15);
	}

	public void clearRoute() {
		mPoints.clear();
	}

	public void drawPath(MapView mv, Canvas canvas) {

		Path path = new Path();

		for (int i = 0; i < mPoints.size(); i++) {
			Point point = new Point();
			mv.getProjection().toPixels(mPoints.get(i), point);
			if (i == 0) {
				path.moveTo(point.x, point.y);
			} else {
				path.lineTo(point.x, point.y);
			}
		}

		if (!path.isEmpty()) {
			canvas.drawPath(path, paintDraw);
		}

		path = null;
	}
}
